package com.app.shop.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.app.shop.config.security.DgbSecurityUserHelper;
import com.app.shop.controller.system.BaseController;
import com.app.shop.entity.WgTearm;

import com.app.shop.entity.query.WgTearmQuery;
import com.app.shop.entity.show.BaseShow;
import com.app.shop.entity.show.WgTearmShow;
import com.app.shop.service.shop.WgTearmService;
import com.app.shop.util.ApiRest;
import com.app.shop.util.ClassUtil;
import com.app.shop.util.Constant;
import com.app.shop.util.SysModel;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 教练服务类Controller
 *
 * @author  chen li wei
 * @version 21:14 2019-5-10
 */
@Controller
@RequestMapping(value = "/wgTearm")
public class WgTearmController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(WgTearmController.class);
    @Autowired
    WgTearmService wgTearmService;

    /**
     * 查询教练列表
     * @param wgTearmQuery 查询教练参数
     * @return 返回教练列表
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/getTearmList")
    public ApiRest getDictList(@RequestBody WgTearmQuery wgTearmQuery) {
        log.debug(JSONObject.toJSON(wgTearmQuery).toString());
        //查询
        Page<WgTearm> wgTearmList=wgTearmService.getWgTearmList(wgTearmQuery);
        //查询结果对象置换
        List<WgTearmShow> resList=new ArrayList<WgTearmShow>();
        WgTearmShow wgTearmShow;
        //数据转换
        for(WgTearm wgTearm:wgTearmList){
            wgTearmShow=new WgTearmShow();
            try {
                ClassUtil.fatherToChild(wgTearm,wgTearmShow);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //码表值补全
            wgTearmShow.dataSet();
            resList.add(wgTearmShow);
        }
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE,
                new BaseShow(wgTearmList.getPageNum(), wgTearmList.getPageSize(), wgTearmList.getTotal(), wgTearmList.getPages(), resList));    }

    /**
     * 获取教练信息
      * @param wgTearm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getTearmById")
    public ApiRest getTearmById(@RequestBody WgTearm wgTearm) {
        log.debug(JSONObject.toJSON(wgTearm).toString());
        wgTearm  = wgTearmService.getTearmById(wgTearm);
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, wgTearm);
    }



    /**
     * 教练删除
     * @param wgTearm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "editTearmDelete")
    public ApiRest editTearmDelete(@RequestBody WgTearm wgTearm) {
        log.debug(JSONObject.toJSON(wgTearm).toString());
        wgTearm.setModifiedUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        wgTearm.setModifiedDate(new Date());
        SysModel sysModel = wgTearmService.editTearmDelete(wgTearm);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }


    /**
     * 添加教练
     * @param wgTearm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addTearm")
    public ApiRest addTearm(@RequestBody WgTearm wgTearm) {
        log.debug(JSONObject.toJSON(wgTearm).toString());
        wgTearm.setCreateUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        wgTearm.setCreatedDate(new Date());
        SysModel sysModel = wgTearmService.addTearm(wgTearm);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }

    /**
     * 教练信息修改
     *
     * @param wgTearm 需要修改的教练信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "editTearm")
    public ApiRest editTearm(@RequestBody WgTearm wgTearm) {
        log.debug(JSONObject.toJSON(wgTearm).toString());
        wgTearm.setModifiedUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        SysModel sysModel = wgTearmService.editTearm(wgTearm);
        wgTearm.setModifiedDate(new Date());
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }


    /**
     * 教练上架
     * @param wgTearm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "editTearmUp")
    public ApiRest editTearmUp(@RequestBody WgTearm wgTearm) {
        log.debug(JSONObject.toJSON(wgTearm).toString());
        wgTearm.setModifiedUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        wgTearm.setModifiedDate(new Date());
        SysModel sysModel = wgTearmService.editTearmUp(wgTearm);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }

    /**
     * 教练下架
     * @param wgTearm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "editTearmDown")
    public ApiRest editTearmDown(@RequestBody WgTearm wgTearm) {
        log.debug(JSONObject.toJSON(wgTearm).toString());
        wgTearm.setModifiedUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        wgTearm.setModifiedDate(new Date());
        SysModel sysModel = wgTearmService.editTearmDown(wgTearm);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }



}
